package com.aids_malaria.ui.report;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aids_malaria.R;
import com.aids_malaria.adapter.MedicalTreatmentAdapter;
import com.aids_malaria.base.BaseActivity;
import com.aids_malaria.base.BaseQuickAdapter;
import com.aids_malaria.event.Events;
import com.aids_malaria.http.BaseObserver;
import com.aids_malaria.http.RetrofitFactory;
import com.aids_malaria.http.RxScheduler;
import com.aids_malaria.http.api.RetrofitApi;
import com.aids_malaria.http.response.MedicalTreatment;
import com.aids_malaria.ui.datacollect.CountySelectActivity;
import com.aids_malaria.ui.datacollect.DataQueryActivity;
import com.aids_malaria.util.GsonUtil;
import com.aids_malaria.util.LogUtil;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MedicalTreatmentActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refresh_layout;
    @BindView(R.id.rv_data)
    RecyclerView rv_data;
    @BindView(R.id.et_county_name)
    EditText et_county_name;
    @BindView(R.id.et_medical_num)
    EditText et_medical_num;

    private MedicalTreatmentAdapter medicalTreatmentAdapter;
    private List<MedicalTreatment> mData = new ArrayList<>();

    private String area_code;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_medical_treatment;
    }

    @Override
    protected void initView() {
        super.initView();
        tv_title.setText(getString(R.string.medical_treatment2));
        initRefreshLayout();
    }

    @Override
    protected void initData() {
        super.initData();
        initRecycleView();
        //一进来不加载数据，防止数据量大
//        getMedicalData();
    }

    @OnClick({R.id.ll_back, R.id.iv_search, R.id.iv_select})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.iv_search:
                getMedicalData();
                break;
            case R.id.iv_select:
                // 选择村
                Bundle bundle = new Bundle();
                bundle.putInt(Events.TYPE_KEY, Events.TYPE_MEDICAL_TREATMENT);
                startActivity(CountySelectActivity.class, bundle);
                break;
        }
    }

    /**
     * 县选择回调
     *
     * @param countySelectEvent
     */
    @Subscribe
    public void onCodeEvent(Events.CountySelectEvent countySelectEvent) {
        if (countySelectEvent.getType() == Events.TYPE_MEDICAL_TREATMENT) {
            et_county_name.setText(countySelectEvent.getAreaC().getCounty_name());
            area_code = countySelectEvent.getAreaC().getCounty_code();
            getMedicalData();
        }
    }

    private void initRefreshLayout() {
        refresh_layout.setEnableLoadMore(false);
        refresh_layout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refresh_layout.finishLoadMore(1500);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh_layout.finishRefresh(1000);
                getMedicalData();
            }
        });
    }

    private void initRecycleView() {
        rv_data.setLayoutManager(new LinearLayoutManager(mContext));
        medicalTreatmentAdapter = new MedicalTreatmentAdapter(mContext, R.layout.item_medical_treatment, mData);
        rv_data.setAdapter(medicalTreatmentAdapter);
        medicalTreatmentAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                MedicalTreatment medicalTreatment = mData.get(position);
                Intent intent = new Intent(MedicalTreatmentActivity.this, DataQueryActivity.class);
                intent.putExtra("MedicalTreatment", medicalTreatment);
                intent.putExtra("county_name", et_county_name.getText().toString().trim());
                intent.putExtra(DataQueryActivity.AREA_CODE, medicalTreatment.getArea_code());
                startActivity(intent);
            }
        });
    }

    private void getMedicalData() {
        RetrofitFactory.getInstance().createApi(RetrofitApi.class).getMedicalData(area_code == null ? "" : area_code, et_medical_num.getText().toString().trim())
                .compose(RxScheduler.rxSchedulerTransform())
                .subscribe(new BaseObserver<List<MedicalTreatment>>() {

                    @Override
                    protected void onStart() {
                        getLoadingDialog().show();
                    }

                    @Override
                    protected void onSuccess(List<MedicalTreatment> data) throws Exception {
                        if (data != null) {
                            mData.clear();
                            mData.addAll(data);
                            medicalTreatmentAdapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    protected void onFinish() {
                        getLoadingDialog().dismiss();
                    }
                });
    }

}
